package com.appspot.graph_wave;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class GadgetServlet extends HttpServlet {
	private static final String gadgetXML =
		  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
		+ "<Module>\n"
		+ "<ModulePrefs title=\"Graphviz Wave\" height=\"320\">\n"
		+ "    <Require feature=\"wave-preview\" />\n"
		+ "</ModulePrefs>\n"
		+ "<Content type=\"html\">\n"
		+ "    <![CDATA[\n"
		+ "        <p>Graphy has been taken offline and is looking for a new maintainer."
		+ "           Please see the <a href=\"http://code.google.com/p/graph-wave/\">project page</a>"
		+ "           if you are interested in maintaining Graphy.</p>"
		/*
		+ "        <script type=\"text/javascript\">\n"
		+ "            function buttonDotClicked() {\n"
		+ "                wave.getState().submitDelta({'url' : '@@DOT@@'});\n"
		+ "            }\n"
		+ "            function buttonNeatoClicked() {\n"
		+ "                wave.getState().submitDelta({'url' : '@@NEATO@@'});\n"
		+ "            }\n"
		+ "            function buttonCircoClicked() {\n"
		+ "                wave.getState().submitDelta({'url' : '@@CIRCO@@'});\n"
		+ "            }\n"
		+ "\n"
		+ "            function stateUpdated() {\n"
		+ "                var url = wave.getState().get('url');\n"
		+ "                if (url) {\n"
		+ "                    var canvas = document.getElementById('canvas');\n"
		+ "                    canvas.setAttribute('src', url);\n"
		+ "                }\n"
		+ "            }\n"
		+ "\n"
		+ "            function init() {\n"
		+ "                if (wave && wave.isInWaveContainer()) {\n"
		+ "                    wave.setStateCallback(stateUpdated);\n"
		+ "                }\n"
		+ "            }\n"
		+ "            gadgets.util.registerOnLoadHandler(init);\n"
		+ "        </script>\n"
		+ "\n"
		+ "        <embed id=\"canvas\" src=\"@@DOT@@\" width=\"600\" height=\"320\" type=\"image/svg+xml\"\n"
		+ "               pluginspage=\"http://www.adobe.com/svg/viewer/install/\"\n"
		+ "               style=\"border: 1px solid #000000;\" />\n"
		*/
		/*
		+ "        <div>"
		+ "            <input type=button value=\"Dot\" id=\"buttonDot\" onClick=\"buttonDotClicked()\">\n"
		+ "            <input type=button value=\"Neato\" id=\"buttonNeato\" onClick=\"buttonNeatoClicked()\">\n"
		+ "            <input type=button value=\"Circo\" id=\"buttonCirco\" onClick=\"buttonCircoClicked()\">\n"
		+ "        </div>"
		*/
		+ "    ]]>\n"
		+ "</Content>\n"
		+ "</Module>\n";

	private static final Pattern urlPattern = Pattern.compile(".*/(\\d+).xml");
	private static final Pattern replaceIdPattern = Pattern.compile("@@ID@@");
	private static final Pattern replaceDotPattern = Pattern.compile("@@DOT@@");
	private static final Pattern replaceNeatoPattern = Pattern.compile("@@NEATO@@");
	private static final Pattern replaceCircoPattern = Pattern.compile("@@CIRCO@@");
	
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String uri = req.getRequestURI();
		Matcher match = urlPattern.matcher(uri);
		if (match.matches()) {
    		String id = match.group(1);
			String dotUrl = RobotServlet.webdotUrl + RobotServlet.sourceUrl + id + ".gv.dot.svg";
			String neatoUrl = RobotServlet.webdotUrl + RobotServlet.sourceUrl + id + ".gv.neato.svg";
			String circoUrl = RobotServlet.webdotUrl + RobotServlet.sourceUrl + id + ".gv.circo.svg";
    		String idReplaced = replaceIdPattern.matcher(gadgetXML).replaceAll(id);
    		String dotReplaced = replaceDotPattern.matcher(idReplaced).replaceAll(dotUrl);
    		String neatoReplaced = replaceNeatoPattern.matcher(dotReplaced).replaceAll(neatoUrl);
    		String circoReplaced = replaceCircoPattern.matcher(neatoReplaced).replaceAll(circoUrl);
            resp.setContentType("text/xml");
    		resp.getWriter().println(circoReplaced);
        } else {
            resp.setContentType("text/plain");
			resp.getWriter().println("Error: Unable to parse request.");
		}
    }
}
